package com.ds.smartstudio.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ds.smartstudio.controller.DTO.SearchValues;
import com.ds.smartstudio.controller.DTO.DeviceDTO;
import com.ds.smartstudio.entity.Area;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.entity.History;
import com.ds.smartstudio.entity.Type;
import com.ds.smartstudio.mapper.AreaMapper;
import com.ds.smartstudio.mapper.DeviceMapper;
import com.ds.smartstudio.mapper.HistoryMapper;
import com.ds.smartstudio.mapper.TypeMapper;
import com.ds.smartstudio.service.AreaService;
import com.ds.smartstudio.service.DeviceService;
import com.ds.smartstudio.service.HistoryService;
import com.ds.smartstudio.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@Service
@Slf4j
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {


    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private HistoryMapper historyMapper;
    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public List<Object> devicePage(int page, int pageSize, String name) {
        Page<Device> devicePage = new Page<>(page, pageSize);
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ds_device_id")
                .like("ds_device_addr", name)
                .eq("ds_device_status", 1);
        this.page(devicePage, wrapper);
        List<Device> devices = devicePage.getRecords();
        List<DeviceDTO> deviceDTOList = new ArrayList<>();
        devices.forEach(device -> {
            QueryWrapper<Area> areaWrapper = new QueryWrapper<>();
            areaWrapper.eq("ds_area_id", device.getDsAreaId());
            Area area = areaMapper.selectOne(areaWrapper);
            List<Type> types = null;
            if (device.getDsTypeId().size() > 0) {
                QueryWrapper<Type> typeWrapper = new QueryWrapper<>();
                typeWrapper.in("ds_type_id", device.getDsTypeId());
                types = typeMapper.selectList(typeWrapper);
            }
            History history = historyMapper.selectLatestByMac(device.getDsDeviceMac());
            String state = "";
            if (history != null) {
                state = history.getDsHistoryValue();
            }
            deviceDTOList.add(new DeviceDTO(device.getDsDeviceModel(), device.getDsDeviceMac(), device.getDsDeviceAddr(), device.getDsDeviceDisable(), device.getDsDeviceFirmware(), area, types, device.getCreateTime(), device.getModifyTime(), device.getDsDeviceId(), state, device.getDsDeviceStatus()));
        });
        List<Object> data = new ArrayList<>();
        data.add(deviceDTOList);
        data.add(selectTotalByName(name));
        return data;
    }

    @Override
    public Integer selectTotalByName(String name) {
        return deviceMapper.selectTotalByName(name);
    }

    @Override
    public Boolean disabled(Device device) {
        UpdateWrapper<Device> wrapper = new UpdateWrapper<>();
        wrapper.eq("ds_device_id", device.getDsDeviceId())
                .set("ds_device_disable", device.getDsDeviceDisable());
        return this.update(wrapper);
    }
    public Boolean change(Device device){
        UpdateWrapper<Device> wrapper = new UpdateWrapper<>();
        wrapper.eq("ds_device_id", device.getDsDeviceId())
                .set("ds_device_status", device.getDsDeviceStatus());
        return this.update(wrapper);
    }


    @Override
    public List<String> macs() {
        return deviceMapper.macs();
    }

    @Override
    public List<Object> searchDevice(SearchValues searchValues) {
        Page<Device> devicePage = new Page<>(searchValues.getPage(), searchValues.getPageSize());
        QueryWrapper<Device> wrapper = new QueryWrapper<Device>();
        if (searchValues!=null){
            if (searchValues.getDsAreaId() != null) {
                wrapper.eq("ds_area_id",searchValues.getDsAreaId());
            }
            if (searchValues.getDsTypeId()!=null) {
                wrapper.like("ds_type_id", searchValues.getDsTypeId().toString());
            }
            if (searchValues.getDsDeviceRunStatus()!=null) {
                wrapper.eq("ds_device_run_status",searchValues.getDsDeviceRunStatus());
            }
            if (searchValues.getDsDeviceMac()!=null) {
                wrapper.eq("ds_device_mac",searchValues.getDsDeviceMac());
            }
        }
        log.info(wrapper.toString());
        this.page(devicePage,wrapper);
        List<Device> devices = devicePage.getRecords();
        List<DeviceDTO> deviceDTOList = new ArrayList<>();
        devices.forEach(device -> {
            QueryWrapper<Area> areaWrapper = new QueryWrapper<>();
            areaWrapper.eq("ds_area_id", device.getDsAreaId());
            Area area = areaMapper.selectOne(areaWrapper);
            List<Type> types = null;
            if (device.getDsTypeId().size() > 0) {
                QueryWrapper<Type> typeWrapper = new QueryWrapper<>();
                typeWrapper.in("ds_type_id", device.getDsTypeId());
                types = typeMapper.selectList(typeWrapper);
            }
            History history = historyMapper.selectLatestByMac(device.getDsDeviceMac());
            String state = "";
            if (history != null) {
                state = history.getDsHistoryValue();
            }
            deviceDTOList.add(new DeviceDTO(device.getDsDeviceModel(), device.getDsDeviceMac(), device.getDsDeviceAddr(), device.getDsDeviceDisable(), device.getDsDeviceFirmware(), area, types, device.getCreateTime(), device.getModifyTime(), device.getDsDeviceId(), state, device.getDsDeviceStatus()));
        });
        List<Object> listdevicepage=new ArrayList<>();
        listdevicepage.add(deviceDTOList);
//        listdevicepage.add(devicePage.getPages());
        listdevicepage.add(devicePage.getTotal());
        return listdevicepage;
    }

    @Override//根据场所id查找设备类型为温度传感器并返回所有设备信息
    public List<Device> tempDevice(Integer id) {
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("ds_area_id", id);
        List<Device> devices = list(wrapper);
        List<Device> devices1 = new ArrayList<>();
        for (int i = 0; i < devices.size(); i++) {
            Device device = devices.get(i);
            if (device.getDsTypeId().size() > 0) {
                QueryWrapper<Type> typeQueryWrapper = new QueryWrapper<>();
                typeQueryWrapper.in("ds_type_id", device.getDsTypeId());
                List<Type> types = typeMapper.selectList(typeQueryWrapper);
                if (selectTempName(types)) {
                    devices1.add(device);
                }
            }
        }
            return devices1;
    }


    private boolean selectTempName(List<Type> types) {
        for (Type type : types) {
            if (type.getDsTypeName().equals("temperature")) {
                return true;
            }
        }
        return false;
    }
    @Override
    public HashSet<String> selectAreaIDByDeviceTypeID(Integer id) {
        return deviceMapper.selectAreaIDByDeviceTypeID(id);
    }
    @Override
    public List<Object> getList(int page, int pageSize,String name) {

        Page<Device> devicePage = new Page<>(page, pageSize);
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ds_device_id")
                .ne("ds_device_status", 1);
        this.page(devicePage, wrapper);
        List<Device> devices = devicePage.getRecords();
        List<DeviceDTO> deviceDTOList = new ArrayList<>();

        devices.forEach(device -> {
            QueryWrapper<Area> areaWrapper = new QueryWrapper<>();
            areaWrapper.eq("ds_area_id", device.getDsAreaId());
            Area area = areaMapper.selectOne(areaWrapper);
            List<Type> types = null;
            if (device.getDsTypeId().size() > 0) {
                QueryWrapper<Type> typeWrapper = new QueryWrapper<>();
                typeWrapper.in("ds_type_id", device.getDsTypeId());
                types = typeMapper.selectList(typeWrapper);
            }
            History history = historyMapper.selectLatestByMac(device.getDsDeviceMac());
            String state = "";
            if (history != null) {
                state = history.getDsHistoryValue();
            }
            deviceDTOList.add(new DeviceDTO(device.getDsDeviceModel(), device.getDsDeviceMac(), device.getDsDeviceAddr(), device.getDsDeviceDisable(), device.getDsDeviceFirmware(), area, types, device.getCreateTime(), device.getModifyTime(), device.getDsDeviceId(), state, device.getDsDeviceStatus()));
        });
        List<Object> data = new ArrayList<>();
        data.add(deviceDTOList);
        data.add(selectTotalByName(name));
        return data;
    }
}

